package com.zimmur.app.mocolink.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.AppUserMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.UserMoneyForm;
import com.zimmur.app.mocolink.domain.model.AppUser;
import com.zimmur.app.mocolink.domain.model.AppUserExample;
import com.zimmur.app.mocolink.domain.model.UserMoneyLog;
import com.zimmur.app.mocolink.domain.param.AppUserParam;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.AppUserInfo;
import com.zimmur.app.mocolink.domain.vo.AppUserInfoForManage;
import com.zimmur.app.mocolink.service.IAppUserService;
import com.zimmur.app.mocolink.service.IUserMoneyLogService;

@Service
public class AppUserServiceImpl implements IAppUserService {

	@Autowired
	private AppUserMapper appUserMapper;
	@Autowired
	private IUserMoneyLogService userMoneyLogService;
	
	@Override
	public AppUser selectById(Integer userId) {
		return appUserMapper.selectByPrimaryKey(userId);
	}

	@Override
	public int update(AppUser appUser) {
		return appUserMapper.updateByPrimaryKeySelective(appUser);
	}

	@Override
	public AppUser selectByAccount(String account) {
		AppUserExample example = new AppUserExample();
		example.createCriteria().andMobileNoEqualTo(account);
		List<AppUser> appUsers=appUserMapper.selectByExample(example);
		if(appUsers!= null && appUsers.size()>0){
			return appUsers.get(0);
		}
		return null;
	}

	@Override
	public int add(AppUser appUser) {
		
		return appUserMapper.insertSelective(appUser);
	}

	@Override
	public AppUserInfo selectInfoByUserId(int userId) {
		return appUserMapper.selectInfoByUserId(userId);
	}

	@Override
	public boolean checkNickname(String nickname) {
		AppUserExample example = new AppUserExample();
		example.createCriteria().andNicknameEqualTo(nickname);
		int count = appUserMapper.countByExample(example);
		return count>0;
	}

	@Override
	public PageUtil<AppUserInfoForManage> selectByParam(AppUserParam param) {
		PageUtil<AppUserInfoForManage> page = new PageUtil<>();
		List<AppUserInfoForManage> list= appUserMapper.selectInfoByParam(param);
		int count = appUserMapper.countInfoByParam(param);
		page.setPageNo(param.getPageNo());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public int batchUpdateUserGrade(String userIds, Integer gradeId) {
		String[] idArray = userIds.split(",");
		List<Integer> idList=new ArrayList<>();
		for(String id:idArray){
			idList.add(Integer.valueOf(id));
		}
		if(idList.size()>0){
			return appUserMapper.batchUpdateGrade(idList, gradeId);
		}
		return 0;
	}
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public synchronized int updateMoney(UserMoneyForm form) {
		int flag =0;
		AppUser appUser = appUserMapper.selectByPrimaryKey(form.getAppUserId());
		if(appUser == null){
			return -1;
		}
		UserMoneyLog moneyLog = new UserMoneyLog();
		double oldMoney = appUser.getMoney()==null?0D:appUser.getMoney().doubleValue();
		moneyLog.setType(form.getType());
		moneyLog.setAmount(new BigDecimal(form.getMoney()));
		moneyLog.setUserMoney(new BigDecimal(oldMoney));
		moneyLog.setUserId(form.getAppUserId());
		moneyLog.setRemark(form.getRemark());
		moneyLog.setCreateTime(DateUtils.getNow());
		moneyLog.setDataId(form.getDataId());
		moneyLog.setCreator(form.getAdminName());
		moneyLog.setSerialNumber(form.getSerialNumber());
		double nowMoney;
		switch (form.getType()) {
		case Constant.User_Money_Up:
			nowMoney=oldMoney + form.getMoney();
			appUser.setMoney(new BigDecimal(nowMoney));
			flag = appUserMapper.updateMoney(appUser);
			break;
		case Constant.User_Money_Pay:
			nowMoney=oldMoney - form.getMoney();
			if(nowMoney>=0){
				appUser.setMoney(new BigDecimal(nowMoney));
				flag = appUserMapper.updateMoney(appUser);
			}else{
				flag = -2;
			}
			break;
		case Constant.User_Money_Withdraw:
			nowMoney=oldMoney - form.getMoney();
			if(nowMoney>=0){
				appUser.setMoney(new BigDecimal(nowMoney));
				flag = appUserMapper.updateMoney(appUser);
			}else{
				flag = -2;
			}
			break;
		case Constant.User_Money_Refund:
			nowMoney=oldMoney + form.getMoney();
			appUser.setMoney(new BigDecimal(nowMoney));
			flag = appUserMapper.updateMoney(appUser);
			break;

		default:
			break;
		}
		if(flag>0){
			userMoneyLogService.add(moneyLog);
		}
		return flag;
	}
}
